package com.joshluisaac.jacksonlearning;

import java.io.File;
import java.io.IOException;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

/**
 * @author joshua
 * 
 */

/*
 * Convert an “user” object into JSON formatted string, and save it into a file
 * “user.json“.
 */
public class JacksonJavaObjectToJson {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		User user = new User();
		ObjectMapper mapper = new ObjectMapper();

		try {
			mapper.defaultPrettyPrintingWriter().writeValue(
					new File("user.json"), user);
			System.out.println(mapper.defaultPrettyPrintingWriter()
					.writeValueAsString(user));
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
